// 租户管理mock数据
export const tenantData = {
  // 租户列表数据
  tenantList: Array.from({ length: 220 }).map((_, i) => ({
    id: i + 1,
    tenantName: `admin${i + 1}`,
    roomNumber: `${100 + i}`,
    contactPhone: `183613363${i.toString().padStart(2, '0')}`,
    queryPassword: `111111`,
    contactAddress: `地址${i + 1}`,
    mergeBilling: i % 2 === 0 ? '是' : '否',
    creator: `创建人${i % 5}`,
    createTime: `2025-01-12 15:${(i % 60).toString().padStart(2, '0')}:00`,
  })),

  // 租户监控数据 (示例，可根据实际需求扩展)
  tenantMonitor: [
    {
      id: 1,
      tenantName: '张三',
      remainingAmount: '11.123',
      todayConsumption: '24.12',
      todayWater: '1.32',
      status: '在线',
      type: '商业',
      level: '1层'
    },
    {
      id: 2,
      tenantName: '李四',
      remainingAmount: '22.234',
      todayConsumption: '12.34',
      todayWater: '2.45',
      status: '离线',
      type: '住宅',
      level: '2层'
    },
    {
      id: 3,
      tenantName: '王五',
      remainingAmount: '33.345',
      todayConsumption: '34.56',
      todayWater: '3.56',
      status: '在线',
      type: '商业',
      level: '1层'
    },
    {
      id: 4,
      tenantName: '赵六',
      remainingAmount: '44.456',
      todayConsumption: '45.67',
      todayWater: '4.67',
      status: '异常',
      type: '住宅',
      level: '3层'
    },
  ],

  // 新增/编辑租户的默认数据结构
  newTenant: {
    tenantName: '',
    roomNumber: '',
    contactPhone: '',
    queryPassword: '',
    contactAddress: '',
    mergeBilling: '否',
    remark: '',
  }
}

// API接口模拟函数
export const tenantApi = {
  // 获取租户列表
  getTenantList(params = {}) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const { page = 1, pageSize = 10, tenantName, roomNumber } = params
        let filteredData = tenantData.tenantList

        if (tenantName) {
          filteredData = filteredData.filter(item => item.tenantName.includes(tenantName))
        }
        if (roomNumber) {
          filteredData = filteredData.filter(item => item.roomNumber.includes(roomNumber))
        }

        const start = (page - 1) * pageSize
        const end = start + pageSize
        const list = filteredData.slice(start, end)
        
        resolve({
          code: 0,
          message: 'success',
          data: {
            list: list,
            total: filteredData.length,
            page,
            pageSize
          }
        })
      }, 500)
    })
  },

  // 获取租户详情
  getTenantDetail(id) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const tenant = tenantData.tenantList.find(item => item.id === id)
        if (tenant) {
          resolve({
            code: 0,
            message: 'success',
            data: tenant
          })
        } else {
          resolve({
            code: 500,
            message: 'Tenant not found'
          })
        }
      }, 300)
    })
  },

  // 新增租户
  addTenant(tenant) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const newId = tenantData.tenantList.length > 0 ? Math.max(...tenantData.tenantList.map(t => t.id)) + 1 : 1
        const newTenant = { ...tenant, id: newId, createTime: new Date().toLocaleString() }
        tenantData.tenantList.push(newTenant)
        resolve({
          code: 0,
          message: '新增成功',
          data: newTenant
        })
      }, 300)
    })
  },

  // 更新租户
  updateTenant(tenant) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const index = tenantData.tenantList.findIndex(item => item.id === tenant.id)
        if (index !== -1) {
          tenantData.tenantList.splice(index, 1, { ...tenantData.tenantList[index], ...tenant })
          resolve({
            code: 0,
            message: '更新成功',
            data: tenant
          })
        } else {
          resolve({
            code: 500,
            message: 'Tenant not found'
          })
        }
      }, 300)
    })
  },

  // 删除租户
  deleteTenant(id) {
    return new Promise((resolve) => {
      setTimeout(() => {
        const initialLength = tenantData.tenantList.length
        tenantData.tenantList = tenantData.tenantList.filter(item => item.id !== id)
        if (tenantData.tenantList.length < initialLength) {
          resolve({
            code: 0,
            message: '删除成功'
          })
        } else {
          resolve({
            code: 500,
            message: 'Tenant not found'
          })
        }
      }, 300)
    })
  },

  // 获取租户监控数据
  getTenantMonitorData() {
    return new Promise((resolve) => {
      setTimeout(() => {
        resolve({
          code: 0,
          message: 'success',
          data: tenantData.tenantMonitor
        })
      }, 300)
    })
  }
}

export default {
  tenantData,
  tenantApi
}


